package com.tanhua.dubbo.api.interceptor;

import com.tanhua.model.domain.User;

/**
 * 用一个ThreadLocal存放数据
 */
public class UserHolder {
    /**
     * 1 创建一个ThreadLocal存放数据
     */
    private static ThreadLocal<User> threadLocal = new ThreadLocal<>();

    /**
     * 2 存数据
     */
    public static void set(User user){
        threadLocal.set(user);
    }
    /**
     * 3 取出数据
     */
    public static User get(){
        return threadLocal.get();
    }

    /**
     * 4 取出里面的id
     */
    public static Long getId(){
        if (threadLocal.get()==null){
            return null;
        }
        return threadLocal.get().getId();
    }


    /**
     * 5 移除数据
     */
    public static void remove(){
        threadLocal.remove();
    }
}
